package com.bz.xtcx.manager.service.impl;

import com.bz.xtcx.manager.comom.XtcxException;
import com.bz.xtcx.manager.entity.*;
import com.bz.xtcx.manager.enums.UserTypeEnum;
import com.bz.xtcx.manager.mapper.CmsSharePlatformMapper;
import com.bz.xtcx.manager.persist.conditions.SharePlatformCondition;
import com.bz.xtcx.manager.service.ICmsSharePlatformService;
import com.bz.xtcx.manager.utils.ExcelUtil;
import com.bz.xtcx.manager.vo.VoQuery;
import com.bz.xtcx.manager.vo.VoResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.io.OutputStream;
import java.util.List;

@Service
public class CmsSharePlatformService implements ICmsSharePlatformService {
	
	private final Logger logger = LoggerFactory.getLogger(CmsSharePlatformService.class);
	
	@Autowired
	private CmsSharePlatformMapper cmsSharePlatformMapper;

	@Override
	public VoResponse add(CmsSharePlatform e) throws XtcxException {
		VoResponse vo = new VoResponse();
		try {
            int cnt = cmsSharePlatformMapper.insert(e);
            if (0 < cnt) {
                vo.setMessage("成功新增平台设备");
            } else {
                vo.setFail(vo);
                vo.setMessage("新增平台设备失败");
            }
            return vo;
        } catch (Exception ex) {
		    throw new XtcxException("新增平台设备时异常", ex);
        }

	}

	@Override
	public VoResponse del(Integer id) throws XtcxException {
        VoResponse vo = new VoResponse();
        try {
            int cnt = cmsSharePlatformMapper.del(id);
            if (0 < cnt) {
                vo.setMessage("成功删除平台设备");
            } else {
                vo.setFail(vo);
                vo.setMessage("删除平台设备失败");
            }
            return vo;
        } catch (Exception ex) {
            throw new XtcxException("删除平台设备时异常", ex);
        }
	}

	@Override
	public VoResponse getSps(SharePlatformCondition queryCondition, int pageNum, int pageSize) throws XtcxException {
        VoResponse vo = new VoResponse();
        try {
            Page<CmsSharePlatform> page = PageHelper.startPage(pageNum, pageSize);
            cmsSharePlatformMapper.selectByCondition(queryCondition);
            PageInfo<CmsSharePlatform> info = new PageInfo<CmsSharePlatform>(page);
            vo.setData(info);
            return vo;
        } catch (Exception ex) {
            throw new XtcxException("查询平台设备时异常", ex);
        }
	}

    @Override
    public VoResponse getAllSps(SharePlatformCondition queryCondition) throws XtcxException {
        VoResponse vo = new VoResponse();
        try {
            List<CmsSharePlatform> rets = cmsSharePlatformMapper.selectByCondition(queryCondition);
            if (null == rets || 0 >= rets.size()) {
                vo.setFail(vo);
                vo.setMessage("未查询到数据");
            } else {
                vo.setData(rets);
            }
            return vo;
        } catch (Exception ex) {
            throw new XtcxException("查询平台设备时异常", ex);
        }
    }

    @Override
	public VoResponse update(CmsSharePlatform e) throws XtcxException {
        VoResponse vo = new VoResponse();
        try {
            int cnt = cmsSharePlatformMapper.updateByPrimaryKeySelective(e);
            if (0 < cnt) {
                vo.setMessage("成功更新平台设备");
            } else {
                vo.setFail(vo);
                vo.setMessage("更新平台设备失败");
            }
            return vo;
        } catch (Exception ex) {
            throw new XtcxException("更新平台设备时异常", ex);
        }
	}

    @Override
    public VoResponse getById(Integer id) throws XtcxException {
        VoResponse vo = new VoResponse();
        try {
            CmsSharePlatform retVo = cmsSharePlatformMapper.findById(id);
            if (null != retVo) {
                vo.setData(retVo);
            } else {
                vo.setFail(vo);
                vo.setMessage("未获得平台设备");
            }
            return vo;
        } catch (Exception ex) {
            throw new XtcxException("根据ID查询平台设备时异常", ex);
        }
    }

    @Override
    public VoResponse export(SharePlatformCondition queryCondition, OutputStream out) {
        VoResponse voRes = new VoResponse();
        final List<CmsSharePlatform> list = cmsSharePlatformMapper.selectByCondition(queryCondition);
        if (null == list || 0 == list.size()) {
            voRes.setFail(voRes);
            voRes.setMessage("未查询到待导出的信息");
            return voRes;
        }
        ExcelUtil.writeMajorPlatformExcel(list, out);
        return null;
    }
}
